#include "CCamera.h"


CCamera::CCamera()
{
	m_Xpos				= 0;
	m_Ypos				= 0;
	m_PrevXpos			= 0;
	m_PrevYpos			= 0;
	m_nCameraWidth		= 0;
	m_nCameraHeight		= 0;
	m_isAutoMove		= false;
}

CCamera::CCamera(float Xpos, float Ypos, 
				 int width, int height)
{
	m_Xpos				= Xpos;
	m_Ypos				= Ypos;
	m_PrevXpos			= Xpos;
	m_PrevYpos			= Ypos;
	m_nCameraWidth		= width;
	m_nCameraHeight		= height;
	m_isAutoMove		= false;
}

CCamera::~CCamera()
{

}


//-----------------------------------//
// Name : SetCameraView				 //
// Desc : Setting up the camera view //
//-----------------------------------//
void CCamera::SetCameraView( float Xpos , float Ypos , int width , int height )
{
	m_Xpos				= Xpos;
	m_Ypos				= Ypos;
	m_nCameraWidth		= width;
	m_nCameraHeight		= height;
}



//----------------------------//
// Name : GetCameraView		  //
// Desc : Get the Camera View //
//----------------------------//
RECT CCamera::GetCameraView()
{
	RECT r;
	r.left		= m_Xpos;
	r.right		= r.left + m_nCameraWidth;
	r.top		= m_Ypos;
	r.bottom	= r.top + m_nCameraHeight;

	return r;
}


//---------------------------------------------------------//
// Name : GetXpos										   //
// Desc : Get the current Camera X position in world coord //
//---------------------------------------------------------//
float CCamera::GetXpos()
{
	return m_Xpos;
}


//---------------------------------------------------------//
// Name : GetYpos										   //
// Desc : Get the current Camera Y position in world coord //
//---------------------------------------------------------//
float CCamera::GetYpos()
{
	return m_Ypos;
}



//--------------------------------------//
// Name : UpdateCamera					//
// Desc : Move camera along with object	//
//--------------------------------------//
void CCamera::UpdateCamera( CTileLayer *pTiled , float posX , float posY)
{
	int MapMaxWidth		= pTiled->GetCellSize() * pTiled->GetColumn();
	int MapMaxHeight	= pTiled->GetCellSize() * pTiled->GetRow();

	m_PrevXpos			= m_Xpos;
	m_PrevYpos			= m_Ypos;

	m_Xpos				= posX - m_nCameraWidth/2.0f;
	if(pTiled->LimitRect(GetCameraView() ) )
	{
		m_Xpos	= m_PrevXpos;
	}

	m_Ypos				= posY - m_nCameraHeight/2.0f;

	if(pTiled->LimitRect(GetCameraView() ) )
	{
		m_Ypos	= m_PrevYpos;
	}

	if ( ( m_Xpos + m_nCameraWidth ) > MapMaxWidth )
	{
		m_Xpos -= (m_Xpos + m_nCameraWidth ) - MapMaxWidth;
	}
	if ( m_Xpos < 0 )
	{
		m_Xpos += (0 - m_Xpos );
	}
	if ( ( m_Ypos + m_nCameraHeight ) > MapMaxHeight )
	{
		m_Ypos -= ( m_Ypos + m_nCameraHeight ) - MapMaxHeight;
	}
	if ( m_Ypos < 0 )
	{
		m_Ypos += ( 0 - m_Ypos );
	}
}




